package net.wofly.sale.commodity.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import net.wofly.framework.dao.PageWrapper;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Brand;
import net.wofly.sale.commodity.service.IBrandService;
import net.wofly.sale.commodity.validator.BrandValidator;

@Controller
@RequestMapping(value = "/api/brand")
@Api(description = "品牌管理")
public class BrandController {

	@Resource
	IBrandService iBrandService;

	@RequestMapping(value = "/save", method = { RequestMethod.POST})
	@ResponseBody
	public ReturnBean<Brand> save(@RequestBody Brand brand) {
		String errorMessage = BrandValidator.validateAddBrandParams(brand);
		if (errorMessage.length() > 0) {
			new ReturnBean<>(-1, errorMessage);
		} 
		
		return iBrandService.save(brand);
	}
	
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	@ResponseBody
	public ReturnBean<Brand> update(@RequestBody Brand brand) {
		String errorMessage = BrandValidator.validateUpdateBrandParams(brand);
		if (errorMessage.length() > 0) {
			new ReturnBean<>(-1, errorMessage);
		} 
		
		return iBrandService.update(brand);
	}

	@RequestMapping(value = "/delete", method = { RequestMethod.POST})
	@ResponseBody
	public ReturnBean<String> delete(String brandID) {
		
		String errorMessage = BrandValidator.validateBrandIDParams(brandID);
		if (errorMessage.length() > 0) {
			new ReturnBean<>(-1, errorMessage);
		} 
		
		return iBrandService.delete(brandID);
	}

	@RequestMapping(value = "/getBrand", method = { RequestMethod.POST})
	@ResponseBody
	public ReturnBean<Brand> getBrand(String brandID) {
		String errorMessage = BrandValidator.validateBrandIDParams(brandID);
		if (errorMessage.length() > 0) {
			new ReturnBean<>(-1, errorMessage);
		} 
		
		return iBrandService.getBrand(brandID) ;
	}
	
	@RequestMapping(value = "/findAll", method = { RequestMethod.POST})
	@ApiImplicitParams({@ApiImplicitParam(name = "brandName", value = "品牌名称", required = false, dataType = "String", paramType = "form"),
						@ApiImplicitParam(name = "companyName", value = "所属公司名称", required = false, dataType = "String", paramType = "form"),
						@ApiImplicitParam(name = "pageNum", value = "第几页", required = true, dataType = "Integer", paramType = "form"),
						@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = true, dataType = "Integer", paramType = "form")})
	@ResponseBody
	public ReturnBean<PageWrapper<Brand>> findAll(String brandName, String companyName,
											@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
			 								@RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize) {
		
		return iBrandService.findAllBrandByStatus(brandName ,companyName ,pageNum ,pageSize ,1);
	}

}
